
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_SP_HENT_EJERFORHOLD_LEDER]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EBAS_SP_HENT_EJERFORHOLD_LEDER]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [DBO].[EBAS_SP_HENT_EJERFORHOLD_LEDER] 
	@session varchar(255),
	@leder varchar(255)
WITH ENCRYPTION
AS
	DECLARE @rettighed int,
		@antal int
	
	SET NOCOUNT ON
	SET @antal = 0

	CREATE TABLE #temp ( rett int )
	INSERT INTO #temp (rett)
	EXEC EBAS.DBO.EBAS_SP_SIKKERHED_VERIFICER_SESSION @session	

	SET @rettighed = (SELECT IsNull(MAX(rett),0) FROM #temp)
	
	IF 0 < @rettighed 
	BEGIN
		SET @antal = (SELECT COUNT(*) FROM EBAS_EJERFORHOLD)
		
		SELECT E.GUID AS GUID, E.LEDER AS LEDER, A.GUID AS ADRESSE_GUID, A.GADE AS GADE, POST_NR, [BY], ATTENTION, TELEFON, TELEFAX, E_MAIL, TYPE, @antal AS ANTAL
		FROM EBAS_ADRESSE A, EBAS_ADRESSE_TYPE T, EBAS_POST_NR N, EBAS_EJERFORHOLD E
		WHERE A.ADRESSE_TYPE_GUID = T.GUID
		AND A.POST_NR_GUID = N.GUID
		AND E.GUID = A.EJER_GUID
		AND E.LEDER = @leder
	END
	ELSE
		SELECT E.GUID AS GUID, E.LEDER AS LEDER, A.GUID AS ADRESSE_GUID, A.GADE AS GADE, POST_NR, [BY], ATTENTION, TELEFON, TELEFAX, E_MAIL, TYPE, @antal AS ANTAL
		FROM EBAS_ADRESSE A, EBAS_ADRESSE_TYPE T, EBAS_POST_NR N, EBAS_EJERFORHOLD E
		WHERE A.ADRESSE_TYPE_GUID = T.GUID
		AND A.POST_NR_GUID = N.GUID
		AND E.GUID IS NULL
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
